package com.power.services.menu;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.menu.MenuDAO;
import com.power.model.menu.TreeMenu;
import com.power.model.menu.TreeMenu4Default;
import com.power.model.menu.TsMenu;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuDAO menuDAO;

	public void create(TsMenu menu) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TsMenu.class);
		criteria.add(Restrictions.eq("menupid", menu.getMenupid()));
		Object obj = this.menuDAO.getStatValue(criteria, "ordernum", "max");
		if (null == obj)
			menu.setOrdernum(Integer.valueOf(1));
		else {
			menu.setOrdernum(Integer.valueOf(Integer.valueOf(obj.toString())
					.intValue() + 1));
		}
		this.menuDAO.save(menu);
	}

	public TsMenu read(Integer id) {
		return ((TsMenu) this.menuDAO.load(id));
	}

	public void update(TsMenu transientObject) {
		this.menuDAO.update(transientObject);
	}

	public void delete(TsMenu persistentObject) {
		this.menuDAO.delete(persistentObject);
	}

	public List<TsMenu> findAll(String groupTxt) {
		List<TsMenu> menuList = null;
		try {
			String hql = "from TsMenu m where exists (select 1 from TsGroupMenu g where groupId in ("
					+ groupTxt + ") and g.menuId = m.menuid) and menupid = 0 order by m.ordernum";
			menuList = menuDAO.find(hql);
			if(null != menuList)
				for (Iterator iterator = menuList.iterator(); iterator
						.hasNext();) {
					TsMenu menu = (TsMenu) iterator.next();
					List cl = menuDAO.find("from TsMenu m where exists (select 1 from TsGroupMenu g where groupId in ("
					+ groupTxt + ") and g.menuId = m.menuid) and menupid = "+menu.getId()+" order by m.ordernum");
					Set<TsMenu> treeSet = new LinkedHashSet<TsMenu>();
					if(null != cl)
						for (Iterator iterator2 = cl.iterator(); iterator2
								.hasNext();) {
							TsMenu mm = (TsMenu) iterator2.next();
							treeSet.add(mm);
						}
					menu.setChildrenTrees(treeSet);
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return menuList;
	}
	
	
	public List<TsMenu> findAll() {
		List<TsMenu> menuList = null;
		try {
			/*
			 * exists (select 1 from TsGroupMenu g where groupId in ("
					+ groupTxt + ") and g.menuId = m.menuid) and
			 * */
			String hql = "from TsMenu m where  menupid = 0 order by m.ordernum";
			menuList = menuDAO.find(hql);
			if(null != menuList)
				for (Iterator iterator = menuList.iterator(); iterator
						.hasNext();) {
					TsMenu menu = (TsMenu) iterator.next();
					/**
					 * exists (select 1 from TsGroupMenu g where groupId in ("
					+ groupTxt + ") and g.menuId = m.menuid) and
					 * 
					 */
					List cl = menuDAO.find("from TsMenu m where  menupid = "+menu.getId()+" order by m.ordernum");
					Set<TsMenu> treeSet = new LinkedHashSet<TsMenu>();
					if(null != cl)
						for (Iterator iterator2 = cl.iterator(); iterator2
								.hasNext();) {
							TsMenu mm = (TsMenu) iterator2.next();
							treeSet.add(mm);
						}
					menu.setChildrenTrees(treeSet);
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return menuList;
	}

	public void dropMenu(int menuid, int parentID, int indexof) {
		TsMenu menu = read(new Integer(menuid));
		if (menu.getMenupid().intValue() == parentID) {
			this.menuDAO
					.bulkUpdate("update com.power.model.menu.TsMenu m set m.ordernum=m.ordernum+1 where m.ordernum>="
							+ indexof
							+ " and m.ordernum<="
							+ menu.getOrdernum() + " and m.menupid=" + parentID);

			menu.setOrdernum(new Integer(indexof));
			this.menuDAO.update(menu);
		} else {
			this.menuDAO
					.bulkUpdate("update com.power.model.menu.TsMenu m set m.ordernum=m.ordernum-1 where m.ordernum>="
							+ menu.getOrdernum().intValue()
							+ " and m.menupid="
							+ menu.getMenupid().intValue());
			this.menuDAO
					.bulkUpdate("update com.power.model.menu.TsMenu m set m.ordernum=m.ordernum+1 where m.ordernum>="
							+ indexof + " and m.menupid=" + parentID);
			menu.setMenupid(new Integer(parentID));
			menu.setOrdernum(new Integer(indexof));
			this.menuDAO.update(menu);
		}
	}

	public List<TreeMenu4Default> findForGroup(Integer[] menuIdArrray,
			Integer menuId) {
		List treeList = new ArrayList();
		DetachedCriteria criteria = DetachedCriteria.forClass(TsMenu.class);
		criteria.add(Restrictions.eq("menupid", menuId));
		criteria.add(Restrictions.in("menuid", menuIdArrray));
		criteria.addOrder(Order.asc("ordernum"));
		List menuList = this.menuDAO.findByCriteria(criteria);
		for (int i = 0; i < menuList.size(); ++i) {
			TsMenu menu = (TsMenu) menuList.get(i);
			TreeMenu4Default tm4d = new TreeMenu4Default();
			tm4d.setHref(menu.getUrl());
			tm4d.setId(menu.getMenuid().intValue());
			tm4d.setQtip(menu.getTitle());
			tm4d.setText(menu.getMenuname());
			tm4d.setChildren(new ArrayList());
			if ((menu.getChildrenTrees() != null)
					&& (menu.getChildrenTrees().size() > 0)) {
				tm4d.setChildren(findForGroup(menuIdArrray, menu.getMenuid()));
			}
			treeList.add(tm4d);
		}
		return treeList;
	}

	private List<TreeMenu> findTreeDistrict(Integer menuid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TsMenu.class);
		criteria.add(Restrictions.eq("menupid", menuid));
		criteria.addOrder(Order.asc("ordernum"));
		return this.menuDAO.findByCriteria(criteria);
	}

}